/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.siglo21.ppo.persistence.dao;

import ar.com.siglo21.ppo.model.Autor;
import ar.com.siglo21.ppo.persistence.MySQLConnection;
import ar.com.siglo21.ppo.util.TrabajoIntegradorException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AutorDAOImpl implements AutorDAO {

    @Override
    public void agregar(Autor object) throws TrabajoIntegradorException, SQLException {
        String sql = "Insert into autor(nombre,apellido) values ('" + object.getNombre()+  "','"+object.getApellido()+"')";
        MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public boolean existe(Autor object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void eliminar(Autor object) throws TrabajoIntegradorException, SQLException {

        String sql = "DELETE FROM autor where id=" + object.getId();
        MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public Autor leer(int id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void actualizar(Autor object) throws TrabajoIntegradorException, SQLException {
        String sql="UPDATE AUTOR SET nombre='"+object.getNombre()+"',"+ "apellido='"+object.getApellido()+"'" +" where id="+object.getId();
       MySQLConnection.getDbCon().insertORDeleteORUpdate(sql);
    }

    @Override
    public List<Autor> listarAutores() throws SQLException, TrabajoIntegradorException {
        String sql = "Select * from autor";
        MySQLConnection.getDbCon().query(sql);
        Autor autor;

        List<Autor> listaAutores = new ArrayList();
        ResultSet resulset = MySQLConnection.getDbCon().query(sql);

        while (resulset.next()) {
            autor = new Autor();
            autor.setId(resulset.getInt(1));
            autor.setNombre(resulset.getString(2));
            autor.setApellido(resulset.getString(3));
            listaAutores.add(autor);
        }

        return listaAutores;

    }

}
